package cn.enjoy.jetcache.service;

import cn.enjoy.jetcache.entity.UserDO;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CachePenetrationProtect;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.alicp.jetcache.anno.CreateCache;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author K
 * @date 2021/11/17 15:16
 */
@Service
public class CacheServiceImpl {

    @CreateCache(name = "user:", expire = 100)
    private Cache<Long, UserDO> userCache;
    @CreateCache(name = "UserService.userCache", expire = 100, cacheType = CacheType.BOTH, localLimit = 50)
    private Cache<Long, UserDO> userCache2;


    public void saveUser(UserDO userDO) {
        String name = UUID.randomUUID().toString();
        UserDO queryUser = userCache.get(userDO.getId());
        if (queryUser == null) {
            userDO.setName(name);
            userCache.put(userDO.getId(), userDO);
        } else {
            queryUser.setName(name);
            userCache.put(userDO.getId(), queryUser);
        }

        System.out.println();
    }

    @CachePenetrationProtect
    @Cached(name = "user:", key = "#userDO.id", expire = 1000)
    public UserDO getUser(UserDO userDO) {
        UserDO userCache = new UserDO();
        userCache.setId(userDO.getId());

        return userCache;
    }

}
